package cn.iocoder.yudao.module.dify.controller.admin.difyChat;


import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.dify.api.vo.req.dify.DifyChatRobotStreamDTO;
import cn.iocoder.yudao.module.dify.service.difyChat.DifyChatRobotService;
import io.github.imfangs.dify.client.exception.DifyApiException;
import io.github.imfangs.dify.client.model.chat.AppParametersResponse;
import io.github.imfangs.dify.client.model.chat.ConversationListResponse;
import io.github.imfangs.dify.client.model.chat.MessageListResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

@RestController
@RequestMapping("/api/dify")
@RequiredArgsConstructor
@Tag(name = "管理后台 - dify对话机器人")
public class DifyChatRobotController {


    @Resource
    private DifyChatRobotService difyChatRobotService;

    @GetMapping("/chat/getAppParameters")
    @Operation(summary = "获取应用参数")
    public CommonResult<AppParametersResponse> getAppParameters() throws DifyApiException, IOException {
        return CommonResult.success(difyChatRobotService.getAppParameters(getLoginUserId()));
    }

    @GetMapping("/chat/creat")
    @Operation(summary = "创建对话")
    public CommonResult<String> chatRobotCreat() throws DifyApiException, IOException {
//        String query = "鲁迅和周树人什么关系？";
        return CommonResult.success(difyChatRobotService.chatRobotCreat(getLoginUserId()));
    }

    @PostMapping(value = "/chat/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @Operation(summary = "问答机器人对话-流式")
    public Flux<CommonResult<String>> chatRobotStreaming(@RequestBody DifyChatRobotStreamDTO difyChatRobotStreamDTO) throws DifyApiException, IOException {
//        String query = "鲁迅和周树人什么关系？";
        Flux<CommonResult<String>> stringFlux = difyChatRobotService.chatRobotStreaming(difyChatRobotStreamDTO.getConversationDifyId(), difyChatRobotStreamDTO.getQuery(), getLoginUserId());
        return stringFlux;
    }

    @GetMapping("/chat/conversationList")
    @Operation(summary = "历史会话列表")
    public CommonResult<ConversationListResponse> chatConversationList() throws DifyApiException, IOException {
        return CommonResult.success(difyChatRobotService.chatConversationList(getLoginUserId()));
    }

    @PostMapping("/chat/messageList")
    @Operation(summary = "获取当前会话历史消息")
    public CommonResult<List<MessageListResponse.Message>> chatMessageList(@RequestParam String conversationDifyId) throws DifyApiException, IOException {
        MessageListResponse messageListResponse = difyChatRobotService.chatMessageList(conversationDifyId, getLoginUserId());
        return CommonResult.success(messageListResponse.getData());
    }

    @DeleteMapping("/chat/deleteDifyConversation")
    @Operation(summary = "删除会话")
    public CommonResult<Boolean> deleteDifyConversation(@RequestParam String conversationDifyId) throws DifyApiException, IOException {
        return CommonResult.success(difyChatRobotService.deleteDifyConversation(conversationDifyId, getLoginUserId()));
    }

    @PutMapping("/chat/difyConversationRename")
    @Operation(summary = "重命名会话")
    public CommonResult<Boolean> difyConversationRename(@RequestBody DifyChatRobotStreamDTO difyChatRobotStreamDTO) throws DifyApiException, IOException {
        return CommonResult.success(difyChatRobotService.difyConversationRename(difyChatRobotStreamDTO.getId(), getLoginUserId(), difyChatRobotStreamDTO.getName()));
    }


}
